home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / ConfigFileSrc.lha / ConfigFileSrc12 / RexxLibrary / Funcs / Open&Close.c < prev    next >
Encoding:
Text File  |  1997-10-02  |  4.8 KB  |  163 lines

  1. /*
  2. **        $PROJECT: RexxConfigFile.library
  3. **        $FILE: Open&Close.c
  4. **        $DESCRIPTION: rxcf_Open() and rxcf_Close() functions
  5. **
  6. **        (C) Copyright 1997 Marcel Karas
  7. **             All Rights Reserved.
  8. */
  9.  
  10. IMPORT struct Library *DOSBase;
  11. IMPORT struct Library *CFBase;
  12.  
  13. /****** rexxconfigfile.library/cf_Open ***************************************
  14. *
  15. *   NAME
  16. *        cf_Open -- Open a CF file.
  17. *
  18. *   SYNOPSIS
  19. *        Header = cf_Open(Name [,Mode] [,Flags] [,PuddleSize])
  20. *
  21. *        HEADER/N cf_Open(NAME/A,OMODE,FLAGS,PUDDLESIZE)
  22. *
  23. *   FUNCTION
  24. *        This function create a memory pool with a specify puddlesize or
  25. *        default (2048 bytes), allocate pool memory for the header, open or
  26. *        create a new CF file and check which format type has the file (ascii
  27. *        or short format). And if the flag OFLG_READ_TOO set, the file will
  28. *        be read too.
  29. *
  30. *   INPUTS
  31. *        Name - Name and path of the CF file.
  32. *        Mode - Openmodes:
  33. *
  34. *                OMODE_OLDFILE   - An existing file is opened. Did the file
  35. *                                  not exists the function failed. (default)
  36. *                OMODE_NEWFILE   - A new file will be create.
  37. *                OMODE_READWRITE - Opens a file, but creates it if it didn't
  38. *                                  exist.
  39. *        Flags - extra flags:
  40. *
  41. *                OFLG_READ_TOO   - Reads the file directly after the
  42. *                                  it is open. You didn't need use
  43. *                                  cf_Read().
  44. *
  45. *        PuddleSize - Size of the puddle or NULL for default (2048 bytes).
  46. *
  47. *   RESULT
  48. *        Header - a pointer to an initialized Header, or FALSE if the CF
  49. *                 file could not be opened. In the case of a FALSE return,
  50. *                 the RC var can be read to obtain more information on the
  51. *                 failure.
  52. *
  53. *        RC (rexx variable) - contains an error string.
  54. *
  55. *                 OERR_UNKOWN     - Unkown failure.
  56. *                 OERR_OPEN_FILE  - Couldn't open CF file.
  57. *                 OERR_READ_FILE  - Couldn't read CF file.
  58. *                 OERR_NO_FORMAT  - File isn't in CF format.
  59. *                 OERR_NO_SIZE    - File hasn't a size.
  60. *                 OERR_HEADER_MEM - No memory for Header.
  61. *
  62. *                 cf_Read() errors, only if the OFLG_READ_TOO flag set.
  63. *
  64. *                 RERR_FORMAT       - File has an error in the format
  65. *                                     structure.
  66. *                 RERR_UNKOWN_ITYPE - An unkown item type was found.
  67. *
  68. *   EXAMPLE
  69. *        Header = cf_Open("SYS:test.cfg",,OFLG_READ_TOO)
  70. *        If Header ~= '0' Then Do
  71. *          ...
  72. *          cf_Close(Header)
  73. *        End
  74. *        Else Do
  75. *          Select
  76. *            When RC = OERR_OPEN_FILE    Then EStr = "Couldn't open file!"
  77. *            When RC = OERR_READ_FILE    Then EStr = "Couldn't read file!"
  78. *            When RC = OERR_NO_FORMAT    Then EStr = "File isn't in format!"
  79. *            When RC = OERR_NO_SIZE      Then EStr = "File hasn't a size!"
  80. *            When RC = OERR_HEADER_MEM   Then EStr = "No memory for Header!"
  81. *
  82. *            When RC = RERR_FORMAT       Then EStr = "Error in the format!"
  83. *            When RC = RERR_UNKOWN_ITYPE Then EStr = "Unkown item type!"
  84. *            Otherwise EStr = "Unkown failure!"
  85. *          End
  86. *          Say "cf_Open:" EStr
  87. *        End
  88. *
  89. *        ...
  90. *        
  91. *   SEE ALSO
  92. *        cf_Close(), cf_Read(), cf_Write(), exec.library/CreatePool()
  93. *
  94. ******************************************************************************
  95. *
  96. */
  97.  
  98. BYTE OpenCount = 0;
  99.  
  100. UWORD rxcf_Open ( RX_FUNC_ARGS, STRPTR Name )
  101. {
  102.     ULONG    Error, PuddleSize;
  103.     UBYTE    Mode    = CF_OMODE_OLDFILE;
  104.     CFHeader    * Header;
  105.  
  106.     if ( IsValidArg (RxMsg, 2) )
  107.     {
  108.         Mode = StrToOMode (RXARG2);
  109.         if ( Mode == -1 ) return (RXERR_INVALID_ARG);
  110.     }
  111.  
  112.     if ( IsValidArg (RxMsg, 3) )
  113.     {
  114.         if ( !StrCmp (RXARG3, "OFLG_READ_TOO") )
  115.             Mode |= CF_OFLG_READ_TOO;
  116.         else    return (RXERR_INVALID_ARG);
  117.     }
  118.  
  119.     if ( !( PuddleSize = GetLongArg(5) ) )
  120.         PuddleSize = 0;
  121.  
  122.     if ( Header = cf_OpenPS (Name, Mode, &Error, PuddleSize) )
  123.     {
  124.         OpenCount ++;
  125.         *ResStr = CreateNumArgStrP (Header);
  126.     }
  127.     else    SetErrVar (RxMsg, ERRFUNC_OPEN, Error);
  128.  
  129.     return (RC_OK);
  130. }
  131.  
  132. /****** rexxconfigfile.library/cf_Close **************************************
  133. *
  134. *   NAME
  135. *        cf_Close -- Close a CF file.
  136. *
  137. *   SYNOPSIS
  138. *        cf_Close(Header)
  139. *
  140. *        cf_Close(HEADER/N/A)
  141. *
  142. *   FUNCTION
  143. *        This function close the CF file, deletes the private memory pool
  144. *        and if the HFLG_WRITE_BY_CLOSE and HFLG_CHANGED flags set, the
  145. *        CF file will be write too.
  146. *
  147. *   INPUTS
  148. *        Header - The Header of the file to close.
  149. *
  150. *   SEE ALSO
  151. *        cf_Open(), cf_Read(), cf_Write()
  152. *
  153. ******************************************************************************
  154. *
  155. */
  156.  
  157. UWORD rxcf_Close ( RX_FUNC_ARGS, CFHeader * Header )
  158. {
  159.     OpenCount --;
  160.     cf_Close (Header);
  161.     return (RC_OK);
  162. }
  163.